const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
function getReg(str,fmt) {
    var cfmt = fmt;
    cfmt = cfmt.replace(new RegExp(str + "+", "g"), function (full) { return "(" + full + ")"; }) || "";
    return cfmt.replace(/[yMdhmsS]/g, "\\d");
}
function formatDate(dateStr, fmt) {
    var date = dateStr;
    if (typeof (dateStr)=="string"){
        var year = parseInt((new RegExp(getReg("y", fmt)).exec(dateStr)[1])) || 0;
        var month = parseInt((new RegExp(getReg("M", fmt)).exec(dateStr)[1])) - 1 || 0;
        var day = parseInt((new RegExp(getReg("d", fmt)).exec(dateStr)[1])) || 0;
        var hour = parseInt((new RegExp(getReg("h", fmt)).exec(dateStr)[1])) || 0;
        var minute = parseInt((new RegExp(getReg("m", fmt)).exec(dateStr)[1])) || 0;
        var second = parseInt((new RegExp(getReg("s", fmt)).exec(dateStr)[1])) || 0;
        date = new Date(year, month, day, hour, minute, second);
    }
    var o = {
        "M+": date.getMonth() + 1,                 //月份   
        "d+": date.getDate(),                    //日   
        "h+": date.getHours(),                   //小时   
        "m+": date.getMinutes(),                 //分   
        "s+": date.getSeconds(),                 //秒   
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
        "S": date.getMilliseconds()             //毫秒   
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;   
}

function dateFormat(date, fmt) {
    date = toDate(date);
    var w = date.getDay();
    var d = date.getDate();
    var o = {
        "M+": date.getMonth() + 1,                 //月份   
        "d+": d,                                    //日   
        "h+": date.getHours(),                   //小时   
        "m+": date.getMinutes(),                 //分   
        "s+": date.getSeconds(),                 //秒   
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "Z": Math.ceil((d + 6 - w) / 7),
        "S": date.getMilliseconds()             //毫秒   
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
function toDate(str) {
    if (str && typeof str == "string") {
        str = str.replace(/T/g, ' ');
        var isUTC = str.substr(str.length - 1) === 'Z';
        if (isUTC) {
            str = str.substr(0, str.length - 1);
        }
        var index = str.lastIndexOf('.');
        if (index > -1 && str.substring(0, index).indexOf(':') > -1) {//去掉毫秒
            str = str.substring(0, index);
        }
        var reg1 = /\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}/,
            reg2 = /^\/Date\([0-9]\)\/$/g,
            result;
        if (reg1.test(str)) {
            str = str.replace(/-/g, "/");
            result = new Date(str);
        } else if (reg2.test(str)) {
            var dateNum = str.substr(6);
            dateNum = dateNum.substring(0, dateNum.length - 2);
            result = new Date(parseInt(dateNum));
        } else {
            result = new Date(str.replace(/\-/g, "/"));
        }
        if (isUTC) {
            result = result.addHours(8);
        }
        return result;
    }
    return str;
}
module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  dateFormat: dateFormat
}
